package tr.edu.metu.is.mega.client.user;

import tr.edu.metu.is.mega.client.core.EditDialog;
import tr.edu.metu.is.mega.client.widgets.MegaComboBox;
import tr.edu.metu.is.mega.client.widgets.MegaDateField;
import tr.edu.metu.is.mega.client.widgets.MegaTextField;
import tr.edu.metu.is.mega.common.dto.UserDTO;
import tr.edu.metu.is.mega.common.dto.UserRole;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;

/**
 * Dialog to edit users.
 */
public class UserEditDialog extends EditDialog<UserDTO> {

    private MegaTextField textFieldUserName;

    private MegaTextField textFieldPassword;

    private MegaTextField textFieldName;

    private MegaTextField textFieldSurname;

    private MegaTextField textFieldEmail;

    private MegaDateField textFieldExpiry;

    private MegaTextField textFieldIdentification;

    private FormBinding formBinding;

    private final BeanModelFactory modelFactoryUser;

    /**
     * Default constructor.
     */
    public UserEditDialog() {
        super(UserDTO.class.getName());
        modelFactoryUser = BeanModelLookup.get().getFactory(UserDTO.class);
        setHeading("Edit User");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initForm(FormPanel parent, Object... params) {

        parent.add(createRoleComboBox(UserDTO.FIELD_ROLE), new FormData("70%"));

        textFieldUserName = new MegaTextField(UserDTO.FIELD_USERNAME);
        textFieldUserName.setFieldLabel("User Name");
        parent.add(textFieldUserName, new FormData("96%"));

        textFieldPassword = new MegaTextField(UserDTO.FIELD_PASSWORD);
        textFieldPassword.setFieldLabel("Password");
        textFieldPassword.setPassword(true);
        parent.add(textFieldPassword, new FormData("96%"));

        textFieldName = new MegaTextField(UserDTO.FIELD_NAME);
        textFieldName.setFieldLabel("Name");
        parent.add(textFieldName, new FormData("96%"));

        textFieldSurname = new MegaTextField(UserDTO.FIELD_SURNAME);
        textFieldSurname.setFieldLabel("Surname");
        parent.add(textFieldSurname, new FormData("96%"));

        textFieldIdentification = new MegaTextField(UserDTO.FIELD_IDENTIFICATIONNO);
        textFieldIdentification.setFieldLabel("Identification");
        parent.add(textFieldIdentification, new FormData("96%"));

        textFieldEmail = new MegaTextField(UserDTO.FIELD_EMAIL);
        textFieldEmail.setFieldLabel("E-Mail");
        parent.add(textFieldEmail, new FormData("96%"));

        textFieldExpiry = new MegaDateField(UserDTO.FIELD_EXPIRYDATE);
        textFieldExpiry.setFieldLabel("Expiry Date");
        parent.add(textFieldExpiry, new FormData("60%"));

        formBinding = new FormBinding(parent);
        formBinding.autoBind();

        UserDTO dto = new UserDTO();
        dto.setRole(UserRole.STUDENT);
        formBinding.bind(BeanModelLookup.get().getFactory(UserDTO.class).createModel(dto));

        setSize(350, 320);
    }

    /**
     * Creates user role combobox from {@link UserRole} enum.
     * 
     * @param boundAttribute field to bind
     * @return combobox
     */
    public static MegaComboBox<BeanModel> createRoleComboBox(String boundAttribute) {

        BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(UserRole.class);
        MegaComboBox<BeanModel> comboBoxRole = new MegaComboBox<BeanModel>(boundAttribute);
        ListStore<BeanModel> store = new ListStore<BeanModel>();
        for (UserRole role : UserRole.values()) {
            store.add(beanModelFactory.createModel(role));
        }
        comboBoxRole.setStore(store);
        comboBoxRole.setFieldLabel("Role");
        comboBoxRole.setDisplayField("description");
        comboBoxRole.setEditable(false);
        comboBoxRole.setForceSelection(true);
        comboBoxRole.setTriggerAction(TriggerAction.ALL);

        return comboBoxRole;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dtoToGUI(UserDTO dto) {
        formBinding.bind(modelFactoryUser.createModel(dto));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected UserDTO guiToDTO() {
        BeanModel model = (BeanModel) formBinding.getModel();
        return model.getBean();
    }

}
